package com.xhui.vimarket.zkplus.zul.attr.evt;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zul.Hbox;

import com.xhui.crm.common.Constants;
import com.xhui.crm.common.util.crm.WeakPassword;
import com.xhui.crm.common.util.encrypt.CrmMD5;
import com.xhui.crm.common.util.encrypt.Md5Encrypt;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.zul.attr.ada.AttrPassworcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusil;
import com.xhui.vimarket.zkplus.zul.com.xhui.vimarket.zkplusttrBaseComp;
import com.xhui.vimarket.zkplus.zucom.xhui.vimarket.zkplus.AttrPasswordTwoComp;
import com.xhui.vimarket.zkplus.zul.attr.rule.AttrValueRexRule;

/**
 * 密码验证长度特殊控件.
 * 长度验证配置在param1
 *  
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-5-5
 * @功能说明：
 * 长度验证配置在param1
 */
public class AttrPasswordTwoEncodeValiParamComposer extends AttrPasswordTwoComposer
    implements AttrPasswordTwoIntf {
    
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 6326094864263772273L;
    
    @Override
    public ComponentInfo doBeforeCompose(final Page page, final Component comp,
        final ComponentInfo info) {
        return info;
    }
    
    @Override
    public void doAfterCompose(final Component comp) throws Exception {
        super.doAfterCompose(comp);
    }
    
    /**
     * onCreate.
     */
    public void onCreate() {
    }
    
    /**
     * 验证.
     * @return RetVO
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public RetVO validate() {
        Component root = this.self.getParent();
        String oldVal = AttrPasswordTwoComp.getInitValue(root);
        String newVal = this.getTextbox().getValue() + "";
        String newValTwo = this.getTextboxTwo().getValue() + "";
        if (!newVal.equals(newValTwo)) { // 两次密码不一致
            return RetVO.newInstance(Constants.RET_FALSE, "两次密码输入不一致，请重新设置");
        } else if (!"".equals(newVal) && (oldVal == null || !oldVal.equals(newVal))) { // 有设置，并且修改了值(旧值为空，或者新旧值不同)
            if (WeakPassword.isWeak(newVal)) { // 弱密码限制
                return RetVO.newInstance(Constants.RET_FALSE, "密码设置过于简单，请重新设置");
            }
            Object oparam1 = AttrCompUtil.getCompAttrStr(root, AttrBaseComp.COMP_PARAM1);
            if (oparam1 != null) { // 长度判断
                if (!AttrValueRexRule.match(oparam1 + "", newVal)) {
                    final String hint = StrUtil.isNullOrEmpty(((Hbox) root).getTooltiptext()) ? ""
                        : ":" + ((Hbox) root).getTooltiptext();
                    return RetVO.newInstance(Constants.RET_FALSE, "密码规则不符合" + hint + "，请重新设置");
                }
            }
        }
        return null;
    }
    
    /**
     * 获取值.
     * 不加密的值
     * 
     * @return String
     * @author wuyx
     * 2011-5-6 wuyx 
     */
    public String getValue() {
        RetVO valiVo = this.validate();
        if (valiVo != null && Constants.RET_FALSE.equals(valiVo.getResult())) {
            throw new WrongValueException(this.getTextbox(), valiVo.getRetMsg());
        }
        Component root = this.self.getParent();
        String oldVal = AttrPasswordTwoComp.getInitValue(root);
        String newVal = this.getTextbox().getValue() + "";
        if (StrUtil.isNullOrEmpty(newVal)) {
            return "";
        } else if (newVal.equals(oldVal)) { // 相同，则无需再次加密
            return newVal;
        }
        CrmMD5 iEncrypt = new CrmMD5();
        String pwd = iEncrypt.getMD5ofStr(newVal); // 需要加密
        return pwd;
    }
    
}
